package com.ytjj.qmyx.admin.controller.imController;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.ImLeaveMsg;
import com.ytjj.qmyx.admin.model.request.im.ImLeaveMsgRequest;
import com.ytjj.qmyx.admin.model.vo.ImLeaveMsgVO;
import com.ytjj.qmyx.admin.model.vo.ImSessionInfoVO;
import com.ytjj.qmyx.admin.service.imService.ImLeaveMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 留言相关功能
 */
@RestController
@RequestMapping("/imLeaveMsg")
@Slf4j
public class ImLeaveMsgController {

    @Autowired
    private ImLeaveMsgService imLeaveMsgService;

    /**
     * 查询留言列表
     * @param imLeaveMsgVO
     * @return
     */
    @PostMapping("/findList")
    public CommonResult findList(@RequestBody ImLeaveMsgVO imLeaveMsgVO){
        return CommonResult.success(imLeaveMsgService.findList(imLeaveMsgVO));
    }

    /**
     * 更新处理方式
     * @param record
     * @return
     */
    @PostMapping("/updateFinshType")
    @OperLog(operModule = "IM留言", operDesc = "更新处理方式", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateFinshType(@RequestBody ImLeaveMsg record){
        try {
            Assert.notNull(record.getId(),"id不能为空");
            Assert.isTrue(StringUtils.isNotBlank(record.getFinshType()),"处理方式不能为空");
            int count = imLeaveMsgService.updateFinshType(record);
            if (count == 0){
                return CommonResult.failed("IM留言更新处理方式失败");
            }
        } catch (Exception e) {
            log.error("IM留言更新处理方式出错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

    /**
     * 批量分配留言给客服
     */
    @RequiresPermissions("messageLeft:assign")
    @PostMapping("/batchSetFinshUser")
    @OperLog(operModule = "IM留言", operDesc = "留言分配", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult batchSetFinshUser(@RequestBody ImLeaveMsgRequest imLeaveMsgRequest){
        try {
            Assert.isTrue(CollectionUtils.isNotEmpty(imLeaveMsgRequest.getIds()),"请先勾选留言");
            Assert.isTrue(StringUtils.isNotBlank(imLeaveMsgRequest.getServiceUser()),"客服不能为空");
           imLeaveMsgService.batchSetFinshUser(imLeaveMsgRequest);
        } catch (Exception e) {
            log.error("IM留言分配出错:{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success();
    }

}
